% Quantitative exercise with model with free entry

disp ('Free entry')

%% Parameters

parameters

param.dT=0.0315;

%% Distribution of efficiency and fixed costs of importing and exporting

% Standard normals for productivity and fixed costs
if monly==1
    load Heterogeneity_Importing.mat
else
    load Heterogeneity.mat
end

phi = DrawProductivity(U,param);
fc = DrawFixedCostsImporting(param,V,phi);
fx = DrawFixedCostsExporting(param,Z,phi,fc);

%% TARGET MOMENTS
% Aggregate Import Share,Aggregate Export Share, Share Importers-only, Share Exporters-only, Share Exporters-Importers, Standard dev of log value added,Standard dev of import shares (level),Standard deviation export shares (level), Correlation log va - import shares (level), Correlation import shares- export shares, Correlation log va - export shares (level)
m(1) = 0.36;m(2) = 0.16;m(3) = 0.26;m(4) = 0.07;m(5) = 0.18;m(6) = 1.71;m(7) = 0.27;m(8) = 0.18 ;m(9) = 0.27;m(10) = 0.19;m(11)= 0.15;

param.shareN = 0.3870; param.shareM = 10.6939/100;
% Expenditure share in non-tradables and final good import share

%% Optimal domestic shares and international strategy

optimalshares

%% Targeted moments in the model and statistics on distributions of sales, import and export shares

moments

%% Calibrate varrho and T

% Given distribution of import and export shares, find weight of imported
% final good in preferences (varrho), pre-devaluation wage (w) and transfers
% (T). See calibration appendix.

x0 = [0.1,0.2,0.15];
[w,~,~, varrho,Timplied] = calibrateaNM_byHand_FE(sD, sX, phi,fc,fx,param); % Note: Expenditure share in non-tradables and final good import share are passed via param
param.varrho = varrho; param.T = Timplied;

%% Retrieve aggregate domestic spending (S), price index (P), fixed cost of entry (fe), and other equilibrium objects
% Note: fe is expressed in tradables

[fe, N,SP,SP2,P,RX,M,Iw, sN, sM,Ptot,LN] = GEobjects_FE_pre(w,sD, sX, phi,fc,fx, param);

disp('Expenditure share in non-tradables  Final good import share')
disp([sN sM])

MF= (1-sN)*sM * Iw; % final good imports, MF, deflated by w
TotalExports_norm = TotalExports * (P/w)^((sg-1)*(1-g) ) * (SP/w) * knorm; % exports in domestic labor
TotalImports_norm = TotalImports * (P/w)^((sg-1)*(1-g) ) * (SP/w) * knorm + MF; % total imports in domestic labor
R = SP + RX*w;
zvec =z; yvec=y;
disp('Check balanced trade condition pre devaluation')   
disp(((TotalImports_norm)/(TotalExports_norm +param.T)-1)*100) % excess imports relative to exports (+ transfers) in percentage points; should be zero.

%% Retrieve fixed costs:
[fc_norm,fx_norm,FX_norm,FM_norm,FXM_norm] = NormalizedFC(fc, fx, FX,FM,FXM,SP,P,w,param);
fe_norm = fe;

%% Devaluation       
    
param.T = param.T - param.dT;
               
if glob==1 
    x0=[8.1062    0.1214    0.2945]; P0 = x0(1); SP0 = x0(2);w0=x0(3);       
    [x_sol, MSE] = DevaluationFixedPoint_FE(param, phi, fc_norm, fx_norm,FXM_norm,FX_norm,FM_norm,fe_norm, SDM,SDXM,z,y, P0, SP0,w0,Opt_options);
end

Peq = abs(x_sol(1)); Seq = abs(x_sol(2)); weq = abs(x_sol(3));
        
[PriceChange, Mn, RXn,EqMoments,sD1,omega1,sX1,TotalExports_norm1,TotalImports_norm1,SPn,In,wn,sNn,sMn,Ptotn,LNn,Neq] = DevaluationResults_FE(param, phi, fc_norm, fx_norm,FXM_norm,FX_norm,FM_norm,fe_norm, SDM,SDXM,z,y, P, Peq,Seq,weq);

postdevaluationresults_FE